package jxc.servlet.info.goods;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxc.base.helper.IdGenerator;
import jxc.data.hibernate.dao.GoodsDAO;
import jxc.data.hibernate.dao.ProviderDAO;
import jxc.data.hibernate.pojo.Goods;

/**
 * 添加或更新商品信息
 */
@WebServlet("/info/goods/update")
public class Update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Update() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * 获取更新页面
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		ProviderDAO pDao=new ProviderDAO();
		request.setAttribute("pList", pDao.getAll());
		GoodsDAO gDao=new GoodsDAO();
		Goods good=gDao.getById(id);
		if(good==null)
			good=new Goods(IdGenerator.getGoodsId(), null, null, 0, 1);
		request.setAttribute("g", good);
		request.getRequestDispatcher("/info/goods/update.jsp").forward(request, response);
	}

	/**
	 * 更新商品
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		String id=request.getParameter("id");
		String price=request.getParameter("price");
		String place=request.getParameter("place");
		String provider=request.getParameter("provider");
		String size=request.getParameter("size");
		String _package=request.getParameter("package");
		String produceCode=request.getParameter("produce_code");
		String promitCode=request.getParameter("promit_code");
		String describe=request.getParameter("describe");
		ProviderDAO pDao=new ProviderDAO();
		GoodsDAO gDao=new GoodsDAO();
		Goods good=new Goods(id, pDao.getById(provider),name, Float.parseFloat(price), 1);
		good.setDescription(describe);
		good.setPackage_(_package);
		good.setProduceplace(place);
		good.setSize(size);
		good.setPromitcode(promitCode);
		good.setProductcode(produceCode);
		gDao.saveOrUpdate(good);
		gDao.commit();
		request.setAttribute("message", "更新商品信息成功");
		this.doGet(request, response);
	}

}
